package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"entgo.io/ent/schema/mixin"
)

type RuleExtend struct{}

// SysRule holds the schema definition for the SysRule entity.
type SysRule struct {
	ent.Schema
}

func (SysRule) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{Table: "sys_rule"},
	}
}
func (SysRule) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixin.Time{},
	}
}

// Fields of the SysRule.
func (SysRule) Fields() []ent.Field {
	return []ent.Field{
		field.String("title").SchemaType(map[string]string{
			dialect.MySQL: "varchar(200)", // Override MySQL.
		}).Comment("菜单名称").Unique(),
		field.String("locale").SchemaType(map[string]string{
			dialect.MySQL: "varchar(50)", // Override MySQL.
		}).Default("").Comment("中英文标题key"),
		field.Int64("weight").SchemaType(map[string]string{
			dialect.MySQL: "int(10)", // Override MySQL.
		}).Default(0).Comment("权重"),
		field.Int8("type").SchemaType(map[string]string{
			dialect.MySQL: "tinyint(1)UNSIGNED", // Override MySQL.
		}).NonNegative().Default(1).Comment("类型 1目录 2菜单 3按钮"),
		field.String("icon").SchemaType(map[string]string{
			dialect.MySQL: "varchar(50)", // Override MySQL.
		}).Optional().Comment("菜单图标"),
		field.String("route_name").SchemaType(map[string]string{
			dialect.MySQL: "varchar(100)", // Override MySQL.
		}).Comment("路由地址").Unique(),
		field.String("route_path").SchemaType(map[string]string{
			dialect.MySQL: "varchar(255)", // Override MySQL.
		}).Comment("菜单路径").Unique(),
		field.String("component").SchemaType(map[string]string{
			dialect.MySQL: "varchar(100)", // Override MySQL.
		}).Comment("组件路径").Unique(),
		field.String("redirect").SchemaType(map[string]string{
			dialect.MySQL: "varchar(255)", // Override MySQL.
		}).Default("").Comment("路由重定向地址"),
		field.String("permission").SchemaType(map[string]string{
			dialect.MySQL: "varchar(80)", // Override MySQL.
		}).Default("").Comment("权限标识"),
		field.Bool("is_iframe").Default(false).Comment("是否外链"),
		field.Bool("keepalive").Default(true).Comment("是否缓存"),
		field.Bool("requires_auth").Default(false).Comment("是否需要登录鉴权"),
		field.Bool("hide_in_menu").Default(false).Comment("'是否在左侧菜单中隐藏该项"),
		field.Bool("hide_children_in_menu").Default(false).Comment("'强制在左侧菜单中显示单项"),
		field.Bool("active_menu").Default(false).Comment("'高亮设置的菜单项 0=否1=是"),
		field.Bool("no_affix").Default(false).Comment("''如果设置为true，标签将不会添加到tab-bar中"),
		field.Bool("default").Default(false).Comment("默认状态"),
		field.Time("delete_time").Optional().Comment("删除时间"),
		field.JSON("extent", RuleExtend{}).Optional().Comment("扩展信息"),
	}
}

// Edges of the SysRule.
func (SysRule) Edges() []ent.Edge {
	return []ent.Edge{
		// 角色与角色的父子关系
		edge.To("children", SysRule.Type).
			From("parent").Unique(),
		edge.From("roles", SysRole.Type).
			Ref("rules"),
	}
}

// Indexes of the SysRule.
func (SysRule) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("delete_time"),
		index.Fields("default"),
	}
}
